package com.dx.mes.service.webservice.service;

import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_OK;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_MESG_OK;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.xml.rpc.holders.StringHolder;

import org.springframework.stereotype.Service;

import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.MqOpe;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.facade.webservice.service.IFeprsvxfrService;
import com.dx.mes.facade.webservice.tx.Base.BaseO;
import com.dx.mes.facade.webservice.tx.Brean.EsbwsdlO;
import com.dx.mes.facade.webservice.tx.Brean.EsbwsdlOA;
import com.dx.mes.facade.webservice.tx.Brean.EsbwsdlOB;
import com.dx.mes.facade.webservice.tx.Brean.EsbwsdlOC;
import com.ekingwin.www.esb.HNYG_045_SAP_PR_PP_0_ReserveTransferPost.HNYG045_SAPPR_PP_0_ReserveTransferPost_pttBindingQSServiceLocator;
import com.ekingwin.www.esb.HNYG_045_SAP_PR_PP_0_ReserveTransferPost.HNYG045_SAPPR_PP_0_ReserveTransferPost_pttBindingStub;

/**
 * 预留调拨过账接口处理class
 * @author Vincent
 *
 */
@Service("feprsvxfrService")
public class FeprsvxfrService implements IFeprsvxfrService{

	private LogUtils logUtils;
	@Resource
	private MqOpe mqOpe;


	@Override
	public String subMainProc(String evt_no, String strInTrx) {
		AppContext.clear();
		AppContext.setCurrServiceName(FeprsvxfrService.class.getSimpleName());
		AppContext.setCurrEventNumber(evt_no);
		logUtils = new LogUtils(FeprsvxfrService.class);
		logUtils.info("[InTrx:" + strInTrx + "]");
		String strOutTrx="";
		BaseO outTrx = new BaseO();
		try {
			outTrx = transToErpProc(strInTrx);
		} catch (Exception ex) {
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(ex.toString());
			logUtils.error(Util.stackTraceToString(ex));
		} finally {
			strOutTrx = JacksonUtil.toJSONStr(outTrx);
			logUtils.info("[OutTrx:" + strOutTrx + "]");
		}
		return strOutTrx;
	}
	
	public BaseO transToErpProc(String strInTrx) throws Exception {
		BaseO outTrx = new BaseO();

		List<EsbwsdlOC> iTEM_list = new ArrayList<EsbwsdlOC>();
		EsbwsdlOC iTEM = new EsbwsdlOC();
		iTEM.setDATAINFO(strInTrx);
		iTEM_list.add(iTEM);

		List<EsbwsdlOB> lIST_list= new ArrayList<EsbwsdlOB>();
		EsbwsdlOB lIST = new EsbwsdlOB();
		lIST.setITEM(iTEM_list);
		lIST_list.add(lIST);

		List<EsbwsdlOA> hEAD_list = new ArrayList<EsbwsdlOA>();
		EsbwsdlOA hEAD = new EsbwsdlOA();
		hEAD.setBIZTRANSACTIONID("SAP.PM_HNYG-045_"+ Util.getMillisecondTime());
		hEAD.setCOUNT("1");
		hEAD.setSRVLEVEL("1");
		hEAD.setUSE("");
		hEAD.setCOMMENTS("");
		hEAD_list.add(hEAD);

		EsbwsdlO dATA = new EsbwsdlO();
		dATA.setHEAD(hEAD_list);
		dATA.setLIST(lIST_list);


		//调用ESB Webserivce
		HNYG045_SAPPR_PP_0_ReserveTransferPost_pttBindingQSServiceLocator locator = new HNYG045_SAPPR_PP_0_ReserveTransferPost_pttBindingQSServiceLocator();
		HNYG045_SAPPR_PP_0_ReserveTransferPost_pttBindingStub stub = (HNYG045_SAPPR_PP_0_ReserveTransferPost_pttBindingStub) locator.getHNYG045_SAPPR_PP_0_ReserveTransferPost_pttBindingQSPort();
		StringHolder SIGN = new StringHolder();
		StringHolder message = new StringHolder();
		stub.HNYG045_SAPPR_PP_0_ReserveTransferPost(mqOpe.obj2Xml(dATA, "DATA"), SIGN, message);
		if("S".equals(SIGN.value)){
			outTrx.setRtn_code(RETURN_CODE_OK);
			outTrx.setRtn_mesg(RETURN_MESG_OK);
		}else{
			outTrx.setRtn_code(SIGN.value);
			outTrx.setRtn_mesg(message.value);
		}
		return outTrx;
	}
}
